package com.kujawnode.utils;

import io.netty.util.internal.ObjectUtil;
import org.springframework.util.ObjectUtils;

import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * @version 1.0
 * @Author kujaw
 * @Date 2025/7/17 18:16
 * @注释
 */
public class CacheUtils {

    /**
     * 缓存工具类
     * @param cahcheSelector
     * @param databaseSelector
     * @param cacheSave
     * @param <T>
     * @return
     */
    public static <T> T getCache(
            Supplier<T> cahcheSelector,
            Supplier<T> databaseSelector,
            Consumer<T> cacheSave
    ){
        //1.先查询redis
        T data = cahcheSelector.get();
        if(ObjectUtils.isEmpty(data)){ //如果redis没有数据
            data = databaseSelector.get(); //查询数据库
            if(!ObjectUtils.isEmpty(data)){ //数据库有数据
                cacheSave.accept(data);
            }
        }
        //返回数据
        return data;
    }
}
